﻿/// <reference path="../General/Extensions/Array.js" />
/// <reference path="../Turbo/Turbo.js" />
/// <reference path="Grouping.js" />
/// <reference path="../Data/DataTemplate.js" />

var ____a;

var $IGroupedRepeaterGroupContainer = TA.Turbo.Controls.IGroupedRepeaterGroupContainer = function IGroupedRepeaterGroupContainer()
{
}
$IGroupedRepeaterGroupContainer.MakeInterface();

$IGroupedRepeaterGroupContainer.prototype.get_Groups = function get_Groups()
{
	return new Array();
}
$IGroupedRepeaterGroupContainer.prototype.get_UnusedGroups = function get_UnusedGroups()
{
	return new Array();
}
$IGroupedRepeaterGroupContainer.prototype.get_Items = function get_Items()
{
	return new Array();
}
$IGroupedRepeaterGroupContainer.prototype.get_UnusedItems = function get_UnusedItems()
{
	return new Array();
}
$IGroupedRepeaterGroupContainer.prototype.get_ContainerElement = function get_ContainerElement()
{
	return document.createElement("asd");
}

$IGroupedRepeaterGroupContainer.prototype.Reset = function Reset()
{
}
$IGroupedRepeaterGroupContainer.prototype.CleanUp = function Reset()
{
}









var $BaseGroupedRepeaterGroupContainer = TA.Turbo.Controls.BaseGroupedRepeaterGroupContainer = function BaseGroupedRepeaterGroupContainer()
{
}
$BaseGroupedRepeaterGroupContainer.Inherit($IGroupedRepeaterGroupContainer);

if (____a)
{
	$BaseGroupedRepeaterGroupContainer.prototype = new $IGroupedRepeaterGroupContainer();
}

$BaseGroupedRepeaterGroupContainer.prototype.Reset = function Reset()
{
	this.get_UnusedGroups().AddRange(this.get_Groups());
	this.get_Groups().Clear();

	this.get_UnusedItems().AddRange(this.get_Items());
	this.get_Items().Clear();
}
$BaseGroupedRepeaterGroupContainer.prototype.CleanUp = function CleanUp()
{
	while (this.get_UnusedGroups().length > 0)
	{
		var group = this.get_UnusedGroups().pop();
		group.Hide();
		group.get_Grouping().get_HiddenGroups().push(group);
	}

	var ui = this.get_UnusedItems(), uil = ui.length;
	for (var iItem = 0; iItem < uil; ++iItem)
	{
		if (ui[iItem].get_Visible())
			ui[iItem].Hide();
	}
}
































var $RepeaterGroup = TA.Turbo.Controls.RepeaterGroup = function RepeaterGroup(grouping, id, control, elements)
{
	if (Function.IsInherit(arguments)) return;

	if (____a)
		grouping = new $RepeaterGrouping();

	this.DataTemplate(control, grouping.get_GroupTemplate(), id, elements);

	this._grouping = grouping;

	this._subGroups = new Array();
	this._unusedSubGroups = new Array();

	this._items = new Array();
	this._unusedItems = new Array();

	this._entries = new Array();
}

if (____a)
{
	$RepeaterGroup.prototype = new TA.Turbo.Data.DataTemplate();
	$RepeaterGroup.prototype.DataTemplate = new TA.Turbo.Data.DataTemplate();
	$RepeaterGroup.prototype.BaseGroupedRepeaterGroupContainer = new TA.Turbo.Controls.BaseGroupedRepeaterGroupContainer();

	$RepeaterGroup.prototype._itemsContainer = document.createElement("asd");
	$RepeaterGroup.prototype._grouping = new $RepeaterGrouping();

	$RepeaterGroup.prototype._subGroups = new Array();
	$RepeaterGroup.prototype._unusedSubGroups = new Array();
	$RepeaterGroup.prototype._items = new Array();
	$RepeaterGroup.prototype._unusedItems = new Array();
	$RepeaterGroup.prototype._entries = new Array();
}
$RepeaterGroup.Inherit(TA.Turbo.Data.DataTemplate);
$RepeaterGroup.Inherit(TA.Turbo.Controls.BaseGroupedRepeaterGroupContainer);

$RepeaterGroup.prototype._collection = null;



$RepeaterGroup.prototype.get_Groups = function get_Groups()
{
	return this._subGroups;
}
$RepeaterGroup.prototype.get_UnusedGroups = function get_UnusedGroups()
{
	return this._unusedSubGroups;
}
$RepeaterGroup.prototype.get_Items = function get_Items()
{
	return this._items;
}
$RepeaterGroup.prototype.get_UnusedItems = function get_UnusedItems()
{
	return this._unusedItems;
}
$RepeaterGroup.prototype.get_Entries = function get_Entries()
{
	return this._entries;
}
$RepeaterGroup.prototype.get_ContainerElement = function get_ContainerElement()
{
	return this._itemsContainer;
}
$RepeaterGroup.prototype.get_Grouping = function get_Grouping()
{
	return this._grouping;
}


$RepeaterGroup.prototype.Init = function Init(parentContainer)
{
	this._itemsContainer = this._grouping.GetItemsContainer(this.get_Elements());
	//this._itemsContainer
}
$RepeaterGroup.prototype.Reset = function Reset()
{
	this.BaseGroupedRepeaterGroupContainer.Reset.call(this);

	this._entries.Clear();
}
$RepeaterGroup.prototype.Bind = function Bind()
{
	if (this.get_Visible() && this._entries.length > 0)
	{
		this.DataTemplate.Bind.call(this, this._entries);

		var args = {
			Container: this,
			DataEntry: this._entries
		};
		this._grouping.RaiseEvent("Bind", args);


		for (var iGroup = 0; iGroup < this._subGroups.length; ++iGroup)
		{
			var group = this._subGroups[iGroup];

			group.Bind();
		}
	}
}
$RepeaterGroup.prototype.Hide = function Hide()
{
	this.DataTemplate.Hide.call(this);

	while (this._items.length > 0)
		this._items.pop().Hide();

	while (this._unusedItems.length > 0)
	{
		var item = this._unusedItems.pop();
		if (item.get_Elements()[0].parentNode == this._itemsContainer) // if this item still exists in this group (not "stolen" by some other group)
			item.Hide();
	}
}
$RepeaterGroup.prototype.DisplayEntry = function DisplayEntry(repeater, iEntry)
{
	var item = repeater._LeaseItemGrouped(repeater.get_DefaultItemType(), null, this);
	repeater._SetItemValue(item, iEntry);
}

